package com.wind.homework.tools;

import java.io.*;
import java.util.*;
import java.util.zip.*;

public class ZipUtil {


//	private String zipFileName;      // 目的地Zip文件
//    private String sourceFileName;   //源文件（带压缩的文件或文件夹）

    public void zip(String zipFileName,String sourceFileName) throws Exception{
        //File zipFile = new File(zipFileName);
//        System.out.println("压缩中...");
        //创建zip输出流
        ZipOutputStream out = new ZipOutputStream( new FileOutputStream(zipFileName));
        //创建缓冲输出流
        BufferedOutputStream bos = new BufferedOutputStream(out);
        File sourceFile = new File(sourceFileName);
        //调用函数
        compress(out,bos,sourceFile,sourceFile.getName());
        bos.close();
        out.close();
//        System.out.println("压缩完成");

    }

    public void compress(ZipOutputStream out,BufferedOutputStream bos,File sourceFile,String base) throws Exception{
        out.putNextEntry( new ZipEntry(base) );
        FileInputStream fos = new FileInputStream(sourceFile);
        BufferedInputStream bis = new BufferedInputStream(fos);

        int tag;
        System.out.println(base);
        //将源文件写入到zip文件中
        while((tag=bis.read())!=-1)
        {
            bos.write(tag);
        }
        bis.close();
        fos.close();


    }
}
